#include "../include/gudb.h"
#include "../src/parser/sql_parser.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Testing full CREATE INDEX statement parsing...\n");
    
    const char* sql = "CREATE INDEX idx_test ON users (id)";
    printf("SQL: %s\n", sql);
    
    // Create parser
    parser_t* parser = parser_create(sql);
    if (!parser) {
        printf("Failed to create parser\n");
        return 1;
    }
    
    printf("Parser created successfully\n");
    printf("Initial current_token: type=%d, value=%s\n", 
           parser->current_token ? parser->current_token->type : -1,
           parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
    
    // Try to parse the full statement using parser_parse_statement
    printf("\n=== Calling parser_parse_statement ===\n");
    statement_t* stmt = parser_parse_statement(parser);
    
    if (!stmt) {
        printf("Failed to parse statement\n");
        printf("Parser error: %s\n", parser_get_error(parser) ? parser_get_error(parser) : "No error message");
        
        // Let's check the current parser state
        printf("Current parser state after failure:\n");
        printf("  current_token type: %d\n", parser->current_token ? parser->current_token->type : -1);
        printf("  current_token value: %s\n", parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
        
        parser_destroy(parser);
        return 1;
    }
    
    printf("Successfully parsed statement!\n");
    printf("Statement type: %d (should be %d for STMT_CREATE_INDEX)\n", stmt->type, STMT_CREATE_INDEX);
    
    if (stmt->type == STMT_CREATE_INDEX) {
        printf("✓ Parsed as CREATE INDEX statement\n");
        printf("  Index name: %s\n", stmt->data.create_index.index_name ? stmt->data.create_index.index_name : "NULL");
        printf("  Table name: %s\n", stmt->data.create_index.table_name ? stmt->data.create_index.table_name : "NULL");
        printf("  Column count: %d\n", stmt->data.create_index.column_count);
        printf("  Is unique: %s\n", stmt->data.create_index.unique ? "true" : "false");
        printf("  If not exists: %s\n", stmt->data.create_index.if_not_exists ? "true" : "false");
        
        if (stmt->data.create_index.column_count > 0 && stmt->data.create_index.column_names) {
            for (uint32_t i = 0; i < stmt->data.create_index.column_count; i++) {
                printf("  Column %d: %s\n", i, stmt->data.create_index.column_names[i] ? stmt->data.create_index.column_names[i] : "NULL");
            }
        }
    } else {
        printf("✗ Parsed as different statement type: %d\n", stmt->type);
    }
    
    // Clean up
    statement_destroy(stmt);
    parser_destroy(parser);
    
    return 0;
}